﻿using Dal.IServices;
using Dal.MsSqlDBOne;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using Common.Config;
namespace Dal.Services
{
    public class DbFactory:IDbFactory
    {
        public DbConnection Create(DatBaseType dbType = DatBaseType.MsSql)
        {
            DbConnection conn = null;
            string conString = string.Empty;
            switch (dbType)
            {
                case DatBaseType.MsSql:
                    conString = Configs.GetConnectionString("MsSqlServer");
                    conn = new SqlConnection(conString);
                    break;
                case DatBaseType.MySql:
                    conString = Configs.GetConnectionString("MySqlServer");
                    conn = new MySqlConnection(conString);
                    break;
                default:
                    break;
            }
           
            return conn;
        }

        public IDbConnection GetDb<T>()
        {
            Type model = typeof(T);
            Type baseModel=model.BaseType;
            object dbinstance= Activator.CreateInstance(baseModel);
            IDbConnection res = baseModel.GetMethod("GetDbInstance").Invoke(dbinstance, null) as IDbConnection;
            return res;
        }
       
    }
}
